//
//  BaseMoveView.m
//  dragDemo
//
//  Created by 吳瀾洲 on 2020/9/24.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

#import "BaseMoveView.h"

@interface BaseMoveView ()

@end

@implementation BaseMoveView


- (void)setUp{
    
    self.isMove = NO;
    self.moveRect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    if (self = [super initWithCoder:coder]) {
        [self setUp];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self setUp];
}




- (void)setIsMove:(BOOL)isMove{
    if (_isMove != isMove) {
        _isMove = isMove;
        
        if (isMove) {
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
            [self addGestureRecognizer:pan];
        }else{
            NSArray *targets = [self gestureRecognizers]; //或者使用imageView. gestureRecognizers获取
            for (UIGestureRecognizer *recognizer in targets)
                [self removeGestureRecognizer: recognizer];
        }
        
    }
}

#pragma mark - 手势
- (void)handlePan:(UIPanGestureRecognizer *)rec
{
    
    
    CGFloat topY = self.moveRect.origin.y;
    CGFloat leftX = self.moveRect.origin.x;
    CGFloat KWidth = self.moveRect.size.width;
    CGFloat KHeight = self.moveRect.size.height;
    
    //返回在横坐标上、纵坐标上拖动了多少像素
    CGPoint point=[rec translationInView:self];
    //NSLog(@"%f,%f",point.x,point.y);
    
    CGFloat centerX = rec.view.center.x+point.x;
    CGFloat centerY = rec.view.center.y+point.y;
    
    CGFloat viewHalfH = rec.view.frame.size.height/2;
    CGFloat viewhalfW = rec.view.frame.size.width/2;
    
    //确定特殊的centerY
    if (centerY - viewHalfH < 0 ) {
        centerY = viewHalfH;
    }
    if (centerY + viewHalfH > KHeight ) {
        centerY = KHeight - viewHalfH;
    }
    
    if (centerY - viewHalfH - topY < 0) {
        centerY = topY + viewHalfH;
    }
    
    
    //确定特殊的centerX
    if (centerX - viewhalfW < 0){
        centerX = viewhalfW;
    }
    if (centerX + viewhalfW > KWidth){
        centerX = KWidth - viewhalfW;
    }
    
    if (centerX - viewhalfW - leftX < 0) {
        centerX = viewhalfW + leftX;
    }
    
    rec.view.center=CGPointMake(centerX, centerY);
    
    //拖动完之后，每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
    [rec setTranslation:CGPointMake(0, 0) inView:self];
    
    
    if(rec.state == UIGestureRecognizerStateEnded){ //拖动完成
        NSLog(@"拖动完成");
        if (self.moveItemFinishedBlock) {
            self.moveItemFinishedBlock(self.frame);
        }
    }
    
    /**
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        beginPoint = [pan locationInView:self];
    }else if (pan.state == UIGestureRecognizerStateChanged){
        
        CGPoint nowPoint = [pan locationInView:self];
        
        float offsetX = nowPoint.x - beginPoint.x;
        float offsetY = nowPoint.y - beginPoint.y;
        CGPoint centerPoint = CGPointMake(beginPoint.x + offsetX, beginPoint.y + offsetY);
        
        if (CGPathContainsPoint(pathRef, NULL, centerPoint, NO))
        {
            self.center = centerPoint;
            
        }else{
            if (centerPoint.y>bottomMargin)
            {
                if (centerPoint.x<rightMargin&&centerPoint.x>leftMargin) {
                    self.center = CGPointMake(beginPoint.x + offsetX, bottomMargin);
                }
            }
            else if (centerPoint.y<topMargin)
            {
                if (centerPoint.x<rightMargin&&centerPoint.x>leftMargin) {
                    self.center = CGPointMake(beginPoint.x + offsetX, topMargin);
                }
            }
            else if (centerPoint.x>rightMargin)
            {
                self.center = CGPointMake(rightMargin, beginPoint.y + offsetY);
            }
            else if (centerPoint.x<leftMargin)
            {
                self.center = CGPointMake(leftMargin, beginPoint.y + offsetY);
            }
        }
    }else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateFailed){
        [pan setTranslation:CGPointMake(0, 0) inView:self];
    }
     **/
}

@end
